# 阶段 1: 构建前端项目
FROM node:18-alpine as frontend-build
WORKDIR /app/frontend
COPY frontend-cal/package*.json ./
RUN npm install
COPY frontend-cal/. .
RUN npm run build

# 阶段 2: 构建后端项目
FROM golang:alpine as backend-build
WORKDIR /app/backend
COPY backend-cal/go.mod  ./
RUN go mod download
COPY backend-cal/. .
RUN CGO_ENABLED=0 GOOS=linux go build -o calculator-backend main.go

# 阶段 3: 生成最终镜像
FROM nginx:1.21-alpine
# 复制前端构建产物到 Nginx 静态文件目录
COPY --from=frontend-build /app/frontend/dist /usr/share/nginx/html
# 复制后端可执行文件
COPY --from=backend-build /app/backend/calculator-backend /usr/local/bin/
# 配置 Nginx 反向代理到后端服务
COPY cal-nginx.conf /etc/nginx/conf.d/cal-nginx.conf

# 暴露端口
EXPOSE 80

# 启动后端服务和 Nginx
CMD ["sh", "-c", "/usr/local/bin/calculator-backend & nginx -g 'daemon off;'"]